/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.bean;

import org.ipti.br.tag.bo.ActorBO;
import org.ipti.br.tag.bo.DisciplineBO;
import org.ipti.br.tag.bo.OrganizationBO;
import org.ipti.br.tag.bo.PerformanceFrequencyBO;
import org.ipti.br.tag.bo.PlanBO;
import org.ipti.br.tag.bo.UnityBO;
import org.ipti.br.tag.entity.Actor;
import org.ipti.br.tag.entity.Discipline;
import org.ipti.br.tag.entity.Organization;
import org.ipti.br.tag.entity.PerformanceFrequency;
import org.ipti.br.tag.entity.Plan;
import org.ipti.br.tag.entity.Unity;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.inject.Inject;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

/**
 *
 * @author root
 */
@ManagedBean(name = "performanceFrequency")
@ViewScoped
public class PerformanceFrequencyBean extends AppBaseBean {

    @Inject
    private ActorBO actorBO;
    @Inject
    private DisciplineBO disciplineBO;
    @Inject
    private OrganizationBO organizationBO;
    @Inject
    private PerformanceFrequencyBO performanceFrequencyBO;
    @Inject
    private PlanBO planBO;
    @Inject
    private UnityBO unityBO;
    @ManagedProperty("#{sessionBean}")
    private SessionBean sessionBean;
    private PerformanceFrequency performanceFrequency;
    private Long actorID;
    private Long disciplineID;
    private Long organizationID;
    private Long orgFatherID;
    private Long performanceFrequencyID;
    private Long planID;
    private Long unityFatherID;
    private Long unityID;
    private int year;
    private List<Actor> actorList;
    private List<PerformanceFrequency> performanceFrequencyList;
    private List<SelectItem> disciplineList;
    private List<SelectItem> organizationList;
    private List<SelectItem> planList;
    private List<SelectItem> unityList;
    private List<SelectItem> yearList;
    private final int INITIAL_YEAR = 1986;

    /** Creates a new instance of PerformanceFrequencyBean */
    public PerformanceFrequencyBean() {
    }

    public void generatePerformanceList() {
        boolean isAdded;
        performanceFrequencyList = new ArrayList<PerformanceFrequency>();
        if (planID != 0) {
            actorList = getActorBO().actorsPerUnity(unityID);
            List<PerformanceFrequency> performanceListAux = getPerformanceFrequencyBO().findPerformanceFrequencyPerPlan(planID);
            for (Actor a : actorList) {
                isAdded = false;
                if (performanceListAux.isEmpty()) {
                    PerformanceFrequency pAux = new PerformanceFrequency();
                    pAux.setActor(a);
                    pAux.setPlan(getPlanBO().find(planID));
                    getPerformanceFrequencyBO().create(pAux);
                    performanceFrequencyList.add(pAux);
                } else {
                    for (PerformanceFrequency p : performanceListAux) {
                        if (p.getActor().getPerson().getId().equals(a.getPerson().getId())) {
                            performanceFrequencyList.add(p);
                            isAdded = true;
                        }
                    }
                    if (!isAdded) {
                        PerformanceFrequency pAux = new PerformanceFrequency();
                        pAux.setActor(a);
                        pAux.setPlan(getPlanBO().find(planID));
                        getPerformanceFrequencyBO().create(pAux);
                        performanceFrequencyList.add(pAux);
                    }
                }
            }
        }
    }

    public void savePerformances() {
//        boolean editableList = true;
//        for (PerformanceFrequency p : performanceFrequencyList) {
//            if ((p.getGrade() > 10 || p.getGrade() < 0) || (p.getFrequency() > 100 || p.getFrequency() < 0)) {
//                editableList = false;
//            }
//        }
//
//        if (editableList) {
//            for (PerformanceFrequency p : performanceFrequencyList) {
//                getPerformanceFrequencyBO().edit(p);
//                success = true;
//            }
//        }

        try {
            for (PerformanceFrequency p : performanceFrequencyList) {
                getPerformanceFrequencyBO().edit(p);
            }
            addInfoMessage("performanceForm:searchGrid", "Operação realizada com sucesso", null);
        } catch (Exception ex) {
            addErrorMessage("performanceForm:searchGrid", "Operação não pôde ser realizada", null);
        }
    }

//    public void redirectNewSearch() {
//        redirect("/educational/performance/performance.xhtml?organization=" + orgFatherID);
//    }
    public void loadOrganizationList() {
        organizationID = 0L;
        unityID = 0L;
        disciplineID = 0L;
        planID = 0L;
        year = 0;
        organizationList = new ArrayList<SelectItem>();
        unityList = new ArrayList<SelectItem>();
        disciplineList = new ArrayList<SelectItem>();
        planList = new ArrayList<SelectItem>();
        yearList = new ArrayList<SelectItem>();
        performanceFrequencyList = new ArrayList<PerformanceFrequency>();
        Organization organization = getOrganizationBO().find(orgFatherID);
        organizationList.add(new SelectItem(0L, "Selecione uma Organização"));
        if (!getOrganizationBO().getOrganizationChildren(organization.getId()).isEmpty()) {
            getAllChildren(organization);
        }

//        organizationID = null;      
//        organizationList = new ArrayList<SelectItem>();
//        organizationList.add(new SelectItem(null, "Selecione uma Organização"));
//        for(Organization o : getOrganizationBO().getOrganizationChildren(orgFatherID)) {
//            organizationList.add(new SelectItem(o.getId(), o.getName()));
//        }
//        unityID = null;
//        disciplineID = null;
//        planID = null;
//        year = 0;
//        unityList = new ArrayList<SelectItem>();
//        disciplineList = new ArrayList<SelectItem>();
//        planList = new ArrayList<SelectItem>();
//        yearList = new ArrayList<SelectItem>();
//        performanceFrequencyList = new ArrayList<PerformanceFrequency>();
    }

    public void loadYearList() {
        unityID = 0L;
        disciplineID = 0L;
        planID = 0L;
        year = 0;
        unityList = new ArrayList<SelectItem>();
        disciplineList = new ArrayList<SelectItem>();
        planList = new ArrayList<SelectItem>();
        yearList = new ArrayList<SelectItem>();
        performanceFrequencyList = new ArrayList<PerformanceFrequency>();
        if (organizationID != 0) {
            yearList.add(new SelectItem(0, "Selecione um Ano"));
            for (int i = Calendar.getInstance().get(Calendar.YEAR); i >= INITIAL_YEAR; i--) {
                yearList.add(new SelectItem(i, String.valueOf(i)));
            }
        }
    }

    public void loadUnityList() {
        unityID = 0L;
        disciplineID = 0L;
        planID = 0L;
        unityList = new ArrayList<SelectItem>();
        disciplineList = new ArrayList<SelectItem>();
        planList = new ArrayList<SelectItem>();
        performanceFrequencyList = new ArrayList<PerformanceFrequency>();
        if (year != 0) {
            unityList.add(new SelectItem(0L, "Selecione uma Unidade"));
            for (Unity u : getUnityBO().unityChildrenPerOrganizationYear(
                    getSessionBean().getLoggedActor().getUnity(), getOrganizationBO().find(organizationID), year)) {
                unityList.add(new SelectItem(u.getId(), u.getName()));
            }
        }
    }

    public void loadDisciplineList() {
        disciplineID = 0L;
        planID = 0L;
        disciplineList = new ArrayList<SelectItem>();
        planList = new ArrayList<SelectItem>();
        performanceFrequencyList = new ArrayList<PerformanceFrequency>();
        if (unityID != 0) {
            disciplineList.add(new SelectItem(0L, "Selecione uma Disciplina"));
            for (Discipline d : getDisciplineBO().findAll()) {
                disciplineList.add(new SelectItem(d.getId(), d.getName()));
            }
        }
    }

    public void loadPlanList() {
        planID = 0L;
        planList = new ArrayList<SelectItem>();
        performanceFrequencyList = new ArrayList<PerformanceFrequency>();
        if (disciplineID != 0) {
            planList.add(new SelectItem(0L, "Selecione um Plano"));
            for (Plan p : getPlanBO().findPlanPerOrganizationDisciplineYear(organizationID, disciplineID, year)) {
                planList.add(new SelectItem(p.getId(), p.getSeason().getDescription()));
            }
        }
    }

    public void getAllChildren(Organization organization) {
        List<Organization> organizationAux = getOrganizationBO().getOrganizationChildren(organization.getId());
        for (Organization org : organizationAux) {
            organizationList.add(new SelectItem(org.getId(), org.getName()));
        }
    }

    /*
     * Getters and Setters
     */
    public ActorBO getActorBO() {
        return actorBO;
    }

    public void setActorBO(ActorBO actorBO) {
        this.actorBO = actorBO;
    }

    public DisciplineBO getDisciplineBO() {
        return disciplineBO;
    }

    public void setDisciplineBO(DisciplineBO disciplineBO) {
        this.disciplineBO = disciplineBO;
    }

    public OrganizationBO getOrganizationBO() {
        return organizationBO;
    }

    public void setOrganizationBO(OrganizationBO organizationBO) {
        this.organizationBO = organizationBO;
    }

    public PerformanceFrequencyBO getPerformanceFrequencyBO() {
        return performanceFrequencyBO;
    }

    public void setPerformanceFrequencyBO(PerformanceFrequencyBO performanceFrequencyBO) {
        this.performanceFrequencyBO = performanceFrequencyBO;
    }

    public PlanBO getPlanBO() {
        return planBO;
    }

    public void setPlanBO(PlanBO planBO) {
        this.planBO = planBO;
    }

    public UnityBO getUnityBO() {
        return unityBO;
    }

    public void setUnityBO(UnityBO unityBO) {
        this.unityBO = unityBO;
    }

    public SessionBean getSessionBean() {
        return sessionBean;
    }

    public void setSessionBean(SessionBean sessionBean) {
        this.sessionBean = sessionBean;
    }

    public PerformanceFrequency getPerformanceFrequency() {
        return performanceFrequency;
    }

    public void setPerformanceFrequency(PerformanceFrequency performanceFrequency) {
        this.performanceFrequency = performanceFrequency;
    }

    public Long getActorID() {
        return actorID;
    }

    public void setActorID(Long actorID) {
        this.actorID = actorID;
    }

    public Long getDisciplineID() {
        return disciplineID;
    }

    public void setDisciplineID(Long disciplineID) {
        this.disciplineID = disciplineID;
    }

    public Long getOrganizationID() {
        return organizationID;
    }

    public void setOrganizationID(Long organizationID) {
        this.organizationID = organizationID;
    }

    public Long getOrgFatherID() {
        return orgFatherID;
    }

    public void setOrgFatherID(Long orgFatherID) {
        this.orgFatherID = orgFatherID;
    }

    public Long getPerformanceFrequencyID() {
        return performanceFrequencyID;
    }

    public void setPerformanceFrequencyID(Long performanceFrequencyID) {
        this.performanceFrequencyID = performanceFrequencyID;
    }

    public Long getPlanID() {
        return planID;
    }

    public void setPlanID(Long planID) {
        this.planID = planID;
    }

    public Long getUnityFatherID() {
        return unityFatherID;
    }

    public void setUnityFatherID(Long unityFatherID) {
        this.unityFatherID = unityFatherID;
    }

    public Long getUnityID() {
        return unityID;
    }

    public void setUnityID(Long unityID) {
        this.unityID = unityID;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public List<Actor> getActorList() {
        if (actorList == null) {
            actorList = new ArrayList<Actor>();
        }
        return actorList;
    }

    public void setActorList(List<Actor> actorList) {
        this.actorList = actorList;
    }

    public List<PerformanceFrequency> getPerformanceFrequencyList() {
        return performanceFrequencyList;
    }

    public void setPerformanceFrequencyList(List<PerformanceFrequency> performanceFrequencyList) {
        this.performanceFrequencyList = performanceFrequencyList;
    }

    public List<SelectItem> getDisciplineList() {
        if (disciplineList == null) {
            disciplineList = new ArrayList<SelectItem>();
        }
        return disciplineList;
    }

    public void setDisciplineList(List<SelectItem> disciplineList) {
        this.disciplineList = disciplineList;
    }

    public List<SelectItem> getOrganizationList() {
        if (organizationList == null) {
            loadOrganizationList();
        }
        return organizationList;
    }

    public void setOrganizationList(List<SelectItem> organizationList) {
        this.organizationList = organizationList;
    }

    public List<SelectItem> getPlanList() {
        if (planList == null) {
            planList = new ArrayList<SelectItem>();
        }
        return planList;
    }

    public void setPlanList(List<SelectItem> planList) {
        this.planList = planList;
    }

    public List<SelectItem> getUnityList() {
        if (unityList == null) {
            unityList = new ArrayList<SelectItem>();
        }
        return unityList;
    }

    public void setUnityList(List<SelectItem> unityList) {
        this.unityList = unityList;
    }

    public List<SelectItem> getYearList() {
        if (yearList == null) {
            yearList = new ArrayList<SelectItem>();
        }
        return yearList;
    }

    public void setYearList(List<SelectItem> yearList) {
        this.yearList = yearList;
    }
}
